//Download by http://www.NewXing.com
// LRegistry.cpp: implementation of the CLRegistry class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LRegistry.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLRegistry::CLRegistry()
{

}

CLRegistry::~CLRegistry()
{
	Close();

}

BOOL CLRegistry::CreateKey(HKEY hKeyRoot,LPCTSTR pszPath)
{
	DWORD dw;
	LONG ReturnValue=RegCreateKeyEx(hKeyRoot,pszPath,0L,NULL,
		 REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&m_hKey,&dw);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;
			
}
BOOL CLRegistry::Open(HKEY hKeyRoot,LPCTSTR pszPath)
{
	m_sPath=pszPath;
	LONG ReturnValue=RegOpenKeyEx(hKeyRoot,pszPath,0L,KEY_ALL_ACCESS,&m_hKey);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;

}
void CLRegistry::Close()
{
	if(m_hKey)
	{
		RegCloseKey(m_hKey);
		m_hKey=NULL;
	}
}

BOOL CLRegistry::VerifyKey(HKEY hKeyRoot,LPCTSTR pszPath)
{
	ASSERT(hKeyRoot);
	ASSERT(pszPath);
	LONG ReturnValue=RegOpenKeyEx(hKeyRoot,pszPath,0L,KEY_ALL_ACCESS,&m_hKey);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;


}
BOOL CLRegistry::VerifyKey(LPCTSTR pszPath)
{
	ASSERT(m_hKey);
	LONG ReturnValue=RegOpenKeyEx(m_hKey,pszPath,0L,KEY_ALL_ACCESS,&m_hKey);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;

}
BOOL CLRegistry::VerifyValue(LPCTSTR pszValue)
{
	ASSERT(m_hKey);
	LONG ReturnValue=RegQueryValueEx(m_hKey,pszValue,NULL,NULL,NULL,NULL);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;

}

BOOL CLRegistry::DeleteValue(LPCTSTR pszValue)
{
	ASSERT(m_hKey);
	LONG ReturnValue=RegDeleteValue(m_hKey,pszValue);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;

}
BOOL CLRegistry::DeleteValueKey(HKEY hKeyRoot,LPCTSTR pszPath)
{
	ASSERT(pszPath);
	ASSERT(hKeyRoot);
	LONG ReturnValue=RegDeleteKey(hKeyRoot,pszPath);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=0L;
	m_Info.dwType=0L;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;

}
	
BOOL CLRegistry::Write(LPCTSTR pszKey,int iVal)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwValue;
	dwValue=(DWORD)iVal;
	LONG ReturnValue=RegSetValueEx(m_hKey,pszKey,0L,REG_DWORD,(CONST BYTE*) &dwValue,sizeof(DWORD));
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=sizeof(DWORD);
	m_Info.dwType=REG_DWORD;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;


}
BOOL CLRegistry::Write(LPCTSTR pszKey,DWORD dwVal)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	LONG ReturnValue=RegSetValueEx(m_hKey,pszKey,0L,REG_DWORD,(CONST BYTE*) &dwVal,sizeof(DWORD));
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=sizeof(DWORD);
	m_Info.dwType=REG_DWORD;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;
	
}
BOOL CLRegistry::Write(LPCTSTR pszKey,LPCTSTR pszVal)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	ASSERT(pszVal);
	ASSERT(AfxIsValidAddress(pszVal,strlen(pszVal),FALSE));
	LONG ReturnValue=RegSetValueEx(m_hKey,pszKey,0L,REG_SZ,(CONST BYTE*)pszVal,strlen(pszVal)+1);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=strlen(pszVal)+1;
	m_Info.dwType=REG_SZ;
	if(ReturnValue==ERROR_SUCCESS)
		return TRUE;

	return FALSE;


}
BOOL CLRegistry::Read(LPCTSTR pszKey,int &iVal)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwType;
	DWORD dwSize=sizeof(DWORD);
	DWORD dwDest;
	LONG ReturnValue=RegQueryValueEx(m_hKey,(LPSTR)pszKey,NULL,&dwType,(BYTE*) &dwDest,&dwSize);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=dwSize;
	m_Info.dwType=dwType;
	if(ReturnValue==ERROR_SUCCESS)
	{
		iVal=(int)dwDest;
		return TRUE;
	}

	return FALSE;


}
BOOL CLRegistry::Read(LPCTSTR pszKey,DWORD &dwVal)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwType;
	DWORD dwSize=sizeof(DWORD);
	DWORD dwDest;
	LONG ReturnValue=RegQueryValueEx(m_hKey,(LPSTR)pszKey,NULL,&dwType,(BYTE*) &dwDest,&dwSize);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=dwSize;
	m_Info.dwType=dwType;
	if(ReturnValue==ERROR_SUCCESS)
	{
		dwVal=dwDest;
		return TRUE;
	}

	return FALSE;


}
BOOL CLRegistry::Read(LPCTSTR pszKey,CString &pszVal)
{
	//ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwType;
	DWORD dwSize=200;
	char szString[255];
	LONG ReturnValue=RegQueryValueEx(m_hKey,(LPSTR)pszKey,NULL,&dwType,(BYTE*)szString,&dwSize);
	m_Info.lMessage=ReturnValue;
	m_Info.dwSize=dwSize;
	m_Info.dwType=dwType;
	if(ReturnValue==ERROR_SUCCESS)
	{
		pszVal=szString;
		return TRUE;
	}

	return FALSE;


}
BOOL CLRegistry::EnumKey(CString &nsKey,DWORD index)
{
	ASSERT(m_hKey);
	ASSERT(nsKey);
	LPTSTR szKey=new TCHAR [50];
	LONG ReturnValue=RegEnumKey(m_hKey,index,szKey,50);		
//LONG ReturnValue=RegEnumValue(m_hKey,index,szKey,&dwSize,NULL,&dwType,(BYTE*)szValue,&dwSize);
	if(ReturnValue==ERROR_NO_MORE_ITEMS)
	{
		delete [] szKey;		
		return FALSE;
	}
	if(ReturnValue==ERROR_SUCCESS)
	{
		nsKey=szKey;
		delete [] szKey;
		return TRUE;
	}

	return FALSE;

}
BOOL CLRegistry::EnumValue(CString &pszKey,CString &pszVal,DWORD index)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwType;
	DWORD dwSize=200;
	LPTSTR szKey=new TCHAR [40];
	TCHAR szValue[255];

	LONG ReturnValue=RegEnumValue(m_hKey,index,szKey,&dwSize,NULL,&dwType,(BYTE*)szValue,&dwSize);
	//RegQueryValueEx(m_hKey,(LPSTR)pszKey,NULL,&dwType,(BYTE*)szString,&dwSize);
	//m_Info.lMessage=ReturnValue;
	//m_Info.dwSize=dwSize;
	//m_Info.dwType=dwType;
	if(ReturnValue==ERROR_NO_MORE_ITEMS)
	{
		delete [] szKey;		
		return FALSE;
	}
	if(ReturnValue==ERROR_SUCCESS)
	{
		pszKey=szKey;
		pszVal=szValue;
		delete [] szKey;
		return TRUE;
	}

	return FALSE;

}

BOOL CLRegistry::EnumValueA(CString &pszKey,CString &pszVal,DWORD index)
{
	ASSERT(m_hKey);
	ASSERT(pszKey);
	DWORD dwType;
	DWORD dwSize=200;
	LPTSTR szKey=new TCHAR [40];
	TCHAR szValue[255];

	LONG ReturnValue=RegEnumValue(m_hKey,index,szKey,&dwSize,NULL,&dwType,(BYTE*)szValue,&dwSize);
	//RegQueryValueEx(m_hKey,(LPSTR)pszKey,NULL,&dwType,(BYTE*)szString,&dwSize);
	//m_Info.lMessage=ReturnValue;
	//m_Info.dwSize=dwSize;
	//m_Info.dwType=dwType;
	if(ReturnValue==ERROR_NO_MORE_ITEMS)
	{
		delete [] szKey;		
		return FALSE;
	}
	if(ReturnValue==ERROR_SUCCESS)
	{
		pszKey=szKey;
		pszVal=szValue;
		delete [] szKey;
		return TRUE;
	}

	pszKey=szKey;
	pszVal=szValue;
	delete [] szKey;
	return TRUE;

}





